package xupt.se.ttms.view.play;

import java.awt.Color;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import xupt.se.ttms.model.Movie;
import xupt.se.ttms.model.Play;
import xupt.se.ttms.model.Studio;
import xupt.se.ttms.service.PlaySrv;
import xupt.se.ttms.service.StudioSrv;
import xupt.se.ttms.view.employee.Employee_MainUITmpl;

public class PlayAddUI {
	public static JTextField b2;
	public static JTextField c2;
	public static JTextField d2;
	public static JTextField e2;
	public static JTextField f2;
	public static JTextField g2;
	public static JFrame frame;
	public static String comboBoxselect="";

	public  PlayAddUI(Movie movie,String user)
	{
		frame =  new JFrame();
		frame.setSize(850,500);
		frame.setTitle("增加演出计划");
		frame.setBounds(0, 0, 850, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		ImageIcon icon=new ImageIcon("img/logo.png");
		JLabel x = new JLabel(icon);
		icon.setImage(icon.getImage().getScaledInstance(850,70,Image.SCALE_DEFAULT));
		JPanel a=new JPanel();
		a.setBackground(Color.decode("#FFe4c4"));
		a.setLayout(null);
		x.setBounds(0,0,850,70);
		a.add(x);
		frame.add(a);
		JLabel a1=new JLabel("增加演出计划");
		a1.setBounds(400,80,100,25);
		a.add(a1);
		JLabel h1=new JLabel("电影名称:");
		h1.setBounds(300,130,50,25);
		a.add(h1);
		JLabel h2=new JLabel(movie.getName());
		h2.setBounds(350,130,200,25);
		a.add(h2);
		
		JComboBox<String> studio_name=new JComboBox<>();
		StudioSrv studioSrv=new StudioSrv();
		List<Studio> list=new ArrayList<>();
		try {
			list=studioSrv.FetchAll();
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}
		if(list.size()>0)
		{
			for (Studio studio : list) {
				studio_name.addItem(studio.getName());
			}
		}
		ComboxListen aListener=new ComboxListen();
		studio_name.addItemListener(aListener);
		JLabel a2=new JLabel("演出厅选择:");
		a2.setBounds(300,160,50,25);
		a.add(a2);
		studio_name.setBounds(350,160,200,25);
		comboBoxselect=(String) studio_name.getSelectedItem();
		a.add(studio_name);
		JLabel b1=new JLabel("演出时间:");
		b1.setBounds(300,190,50,25);
		a.add(b1);
		b2=new JTextField();
		b2.setBounds(350,190,200,25);
		a.add(b2);
		JLabel c1=new JLabel("票价：");
		c1.setBounds(300,220,50,25);
		a.add(c1);
		c2=new JTextField();
		c2.setBounds(350,220,200,25);
		a.add(c2);
		
		JButton enter=new JButton("确认添加");
		enter.setBounds(350,280,90,30);
		enter.setFocusPainted(false);
		enter.setMargin(new Insets(0,0,0,0));
		enter.setContentAreaFilled(false);
		enter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				Integer mv_id=movie.getMv_id();
				
			
				String studio_name=comboBoxselect;
				StudioSrv studioSrv=new StudioSrv();
				List<Studio> studios=new ArrayList<>();
				try {
					studios=studioSrv.Fetch(studio_name);
				} catch (Exception e1) {
					// TODO 自动生成的 catch 块
					e1.printStackTrace();
				}
				if(studios.size()<1)
				{
					return;
				}
				Integer studio_id=studios.get(0).getID();
				Integer ticket_count=studios.get(0).getSeatnumber();
				String play_time=b2.getText();
				float play_price=0;
				try {
					play_price=Float.parseFloat(c2.getText());
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				
				if(mv_id==0||studio_id==0||play_time.equals("")||play_price==0)
				{
					JOptionPane.showConfirmDialog(null, "输入不能为空");
				}
				else
				{
					
					Play play=new Play();
					play.setMv_id(mv_id);
					play.setStudio_id(studio_id);
					play.setPlay_time(play_time);
					play.setPlay_price(play_price);
					play.setTicket_count(ticket_count);
					PlaySrv playSrv=new PlaySrv();
					
					try {
						boolean b=playSrv.add(play);
						if(Employee_MainUITmpl.jTable!=null)
						{
							Employee_MainUITmpl.jTable.updateUI();
						}
						//JOptionPane.showConfirmDialog(null, "添加成功");
						frame.dispose();
						Employee_MainUITmpl employee_MainUITmpl=new Employee_MainUITmpl(user);
					
					} catch (Exception e) {
						// TODO 自动生成的 catch 块
						e.printStackTrace();
					}
				}		
			}
		});
		a.add(enter);
		JButton enter1=new JButton("返回");
		enter1.setBounds(460,280,90,30);
		enter1.setFocusPainted(false);
		enter1.setMargin(new Insets(0,0,0,0));
		enter1.setContentAreaFilled(false);
		enter1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				frame.dispose();
				Employee_MainUITmpl employee_MainUITmpl=new Employee_MainUITmpl(user);
			}
		});
		a.add(enter1);
		frame.setVisible(true);
		frame.setLayout(null);
		
		
	}
	class ComboxListen implements ItemListener
	{
		
		@Override
		public void itemStateChanged(ItemEvent e) {
			// TODO 自动生成的方法存根
			comboBoxselect=e.getItem().toString();
			
		}
		
	}
}
